const fs = require("fs");
const path = require("path");

const data = JSON.parse(fs.readFileSync(path.resolve(__dirname, "data.json")));

// console.log(data);
const result = {
  choiceSingle: [],
  choiceMultiple: [],
  judgment: [],
  choiceMultipleIndefinite: [],
  other: [],
};

const regChoiceSingle = /[\((]单选题[)\)]/g;
const regChoiceMultiple = /[\((]多选题[)\)]/g;
const regJudgment = /[\((]判断[)\)]/g;
const regChoiceMultipleIndefinite = /[\((]不定项[)\)]/g;

const getType = (qs) => {
  if (regChoiceMultipleIndefinite.test(qs.question)) {
    return "choiceMultipleIndefinite";
  }
  if (regJudgment.test(qs.question)) {
    return "judgment";
  }
  if (regChoiceSingle.test(qs.question)) {
    return "choiceSingle";
  }
  if (regChoiceMultiple.test(qs.question)) {
    return "choiceMultiple";
  }
  return "other";
};

const typeContent = [
  "(不定项)",
  "(判断)",
  "(单选题)",
  "(多选题)",
  "(不定项)",
  "(判断)",
  "(单选题)",
  "(多选题)",
];

const processTitle = (qs) => {
  const { question } = qs;
  const numberReg = /^([0-9]+)\./;
  const number = question.match(numberReg)[1];
  let title = question;
  let divide = 0;
  let indexOfType = 0;
  let divideRight = question.length;
  for (let i = 0; i < typeContent.length; i++) {
    if (qs.question.indexOf(typeContent[i]) >= 0) {
      indexOfType = qs.question.indexOf(typeContent[i]);
      divide = indexOfType + typeContent[i].length;
      break;
    }
  }
  if (divide >= question.length) {
    divideRight = indexOfType;
    divide = question.indexOf(".") + 1;
  }
  title = question.substring(divide, divideRight).trim();
  return {
    id: number,
    title,
  };
};

const weight = {
  A: 1,
  B: 2,
  C: 4,
  D: 8,
  E: 16,
  F: 32,
  G: 64,
  H: 128,
  I: 256,
  J: 512,
};

const getWeight = (qs, option) => {
  const indexOfDot = option.indexOf(".");
  if (indexOfDot < 0) {
    console.log("选项不含.", qs);
    return 0;
  }
  if (weight[option[0]]) {
    return weight[option[0]];
  }
  console.log(option);
  return 0;
};

const processOption = (qs) => {
  const options = qs.options.map((option) => {
    const _weight = getWeight(qs, option);
    let content = option;
    const indexOfDot = option.indexOf(".");
    if (indexOfDot >= 0) {
      content = option.substring(indexOfDot + 1).trim();
    }
    return {
      weight: _weight,
      content,
    };
  });
  return {
    options,
  };
};

const processAnswer = (qs) => {
  const { answer } = qs;
  if (answer.trim() === "错") return 0;
  if (answer.trim() === "对") return 1;
  return answer.split(",").reduce((sum, item) => {
    let _weight = 0;
    if (weight[item.trim()]) {
      _weight = weight[item.trim()];
    } else {
      console.log("答案有问题", qs);
    }
    return sum + _weight;
  }, 0);
};

data.forEach((qs) => {
  const type = getType(qs);
  const titleAndId = processTitle(qs);
  const optionsObject = processOption(qs);
  const answer = processAnswer(qs);
  const item = {
    type,
    ...titleAndId,
    ...optionsObject,
    answer,
  };
  result[type].push(item);
});

fs.writeFileSync(path.resolve(__dirname, "out3.json"), JSON.stringify(result));
